class: center, middle, inverse, title-slide # Getting map data into R --- layout: true <div class="dk-footer"> <span> <a href="https://rfortherestofus.com/" target="_blank">R for the Rest of Us </a> </span> </div> --- class: center, middle, dk-section-title background-image:url("images/globe-background.jpg") background-size: contain # Making maps is complex ??? Welcome to mapping with R. In this course we're going to cover how to build both static and interactive maps in R, but before we can start making these visualisations we need to understand what data we need to create maps - and how to get that data into R. We'll also cover a little bit of geographic computation with the sf package. It's important to acknowledge from the beginning that making maps is complex - there are multiple steps we need to go through before we can get from the idea of a map to the actual output chart that we want. --- ## R for maps R has packages for every part of the mapping workflow: .pull-left[ - importing map data - wrangling and cleaning map data - computing geographic distances, areas, intersections and more.. - visualising map data ] .pull-right[ <img src='images/sf-logo.gif' width="40%"> <img src='images/mapview-logo.gif' width="40%"> ] ??? But that definitely shouldn't put us of using R to make maps - the community has made excellent packages that acknowledge the complexity of the problem and work together to provide us with an awesome tidyverse friendly workflow for the whole process: from importing and manipulating map data right through to creating visualisations. The vast majority of this work is shouldered by these two packages - sf and mapview. We'll be using these packages throughout the entire course. I'm starting off with an explanation of how complex maps are because I think it's important, and you'll need to understand some things before you can create good and meaningful maps. But in a few minutes, after talking about all the complexity of maps I'll show how in a few lines of code these packages allow us to get usable maps. --- ## Maps flatten a complex world (I) .pull-left[
] .pull-right[ <!-- --> ] ??? Maps are complex because they flatten a complex world. Our planet is an approximate sphere and maps attempt to project the surface of the Earth onto a 2D surface. There are multiple choices we can make about the mathematics we use to project our maps, and each of those result in very different lookings maps. This slide contains 3 of the most popular projections you'll see out in the wild... the easiest way to see the differences between the maps is to compare the relative size of Africa to the United States of America. --- .pull-left[ <blockquote class="twitter-tweet"><p lang="en" dir="ltr">Animating the Mercator projection to the true size of each country in relation to all the others.<br><br>Focusing on a single country helps to see effect best.<a href="https://twitter.com/hashtag/dataviz?src=hash&ref_src=twsrc%5Etfw">#dataviz</a> <a href="https://twitter.com/hashtag/maps?src=hash&ref_src=twsrc%5Etfw">#maps</a> <a href="https://twitter.com/hashtag/GIS?src=hash&ref_src=twsrc%5Etfw">#GIS</a> <a href="https://twitter.com/hashtag/projectionmapping?src=hash&ref_src=twsrc%5Etfw">#projectionmapping</a> <a href="https://twitter.com/hashtag/mapping?src=hash&ref_src=twsrc%5Etfw">#mapping</a> <a href="https://t.co/clpCiluS1z">pic.twitter.com/clpCiluS1z</a></p>— Neil Kaye (@neilrkaye) <a href="https://twitter.com/neilrkaye/status/1050740679008296967?ref_src=twsrc%5Etfw">October 12, 2018</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] .pull-right[ Jakub Nowosad replicated this GIF in `{ggplot2}` on their blog: [nowosad.github.io/post/maps-distortion](http://nowosad.github.io/post/maps-distortion) ] ??? This tweet from Neil Kayye beautifully highlights how the Mercator projection distorts true country sizes, which is very important as this is the projection used by Google Maps - so it's probably the most commonly seen projection in day to day usage. You might enjoy Jakub's blogpost where they replicate this GIF using {ggplot2}. --- ## All maps are wrong... .pull-left[ <iframe width="675" height="406" src="https://www.youtube.com/embed/kIID5FDi2JQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ] .pull-right[ All map projections distort the surface of the Earth to some extent. Some regions or countries may be better represented with specific projections. ] ??? It's actually kind of interesting to understand why map projections distort the surface of the Earth... and I highly recommend watching Vox media's interesting breakdown of this problem - which has the very attention grabbing title: "All maps are wrong" And it's definitely true that some regions or countries may be better represented with specific projections. --- ### ... but CRS 4326 is a good default The Coordinate Reference System (CRS) of a geospatial dataset specifies the projection of the dataset. ??? ... we're going to cover the technical (and code side of) projections in the geographic computations section of the course, but before we can create ANY maps or work with any geospatial dataset we need to establish a default coordinate reference system - or CRS. --- ### ... but CRS 4326 is a good default The Coordinate Reference System (CRS) of a geospatial dataset specifies the projection of the dataset. - WGS84 is the most common CRS used in the world. - It's more usually referred to by the EPSG code 4326 - This "magic number" will appear often in our code ```r quakes %>% st_as_sf(coords = c("long", "lat"), crs = 4326) %>% mapview() ``` ??? ... the world's most commonly used projection is called WGS84, but in our code we'll be referring to it by its EPSG code: 4326... ... you'll see this magic number pop up all of the time throughout the course, and I'll give a detailed explanation of it in the geographic computations section. --- ## Maps flatten a complex world (II) .pull-left[ <center> <img src='images/disputed-territories.png' width="400px"> </center> ] .pull-right[ - There are more than 100 territorial disputes. - There are more than 30 naming disputes, including seas and other bodies of water. - Making maps is political, consider your audience and intent. ] <br> <br> Interactive dispute map: [metrocosm.com/disputed-territories-map](http://metrocosm.com/disputed-territories-map.html) ??? Maps not only flatten the 3D surface of our world, they also flatten the complexity of our sociopolitical borders, country names, and claims of sovereignty. Making maps is unavoidably political, it's important that you consider both your audience and intent when ultimately publishing your maps. I've linked to an interactive map that allows you to explore most (but not all) of the territorial disputes, this might be a useful resource to use when planning how to map a particular region. --- ## Maps flatten a complex world (III) Base maps (or map tiles) live behind the data we add to our maps and add flavour to the map. .pull-left[ <center>
</center> ] .pull-right[ <center>
</center> ] ??? These two maps help me highlight one last way that maps are complex. The first image is a map of the world constructed from satellite imagery - it's a beautiful map, but it would be hard to overlay most types of geospatial data onto this visualisation in a meaningful way. The second image discards these "base maps" or "map tiles" and shows only country borders from a shapefile dataset. It's often convenient not to use base maps when constructing choropleth. I like to think of base maps as adding flavour to a map, they help us make sense of where our data is on the globe. It's important to understand there are hundreds of different map tiles available. --- ## Maps flatten a complex world (III) .pull-left[ <center>
</center> ] .pull-right[
] ??? ... I've constructed two more contrasting maps using different map tiles. The first is a relief map of Mount everest and highlights the contours of the surrounding land. The second is a street map of Cairo, notice that the labels of the map are in the localised language. This is an important feature and benefit of using base maps instead of plain polygons... but remember what I said before about considering the audience of your maps when choosing how to label your geospatial visualisations. --- ## Maps flatten a complex world (IV) Sometimes the maps we want to build require us to manipulate the relative positions of regions. .pull-left[ <center> <img src="getting-map-data-into-r_files/figure-html/unnamed-chunk-13-1.png" width="350px" /> </center> ] .pull-right[ <center> <img src="getting-map-data-into-r_files/figure-html/unnamed-chunk-14-1.png" width="350px" /> </center> ] ??? This is the fourth and final point I want to make about maps flattening a complex world. The USA is a perfect - and extremely common - example of this complexity. The positions of Alaska and Hawaii are not conducive to making neat and tidy square charts. So what we typically do is relocate these two regions below the contiguous states. We're fortunate that the R ecosystem contains R packages that contain pre-manipulated shapefiles for many of these situations. --- class: center, middle, dk-section-title background-image:url("images/desk-office-pen-ruler.jpg") background-size: contain ## Are maps worth it? ??? I've highlighted many ways that maps are complex to make, so I think it's sensible to question - are maps worth all of this effort? Are there alternative visualisations we could use? --- ### Choropleth are the gold standard for election data .pull-left[ <!-- --> ] .pull-right[ <!-- --> ] ??? Often, maps are the very best choice for visualising election data. And specifically, choropleth have become the gold standard for visualising election results. Choropleth is the technical name for a shaded area map. This slide contains two choropleth visualising the results of two famous votes held in 2016 - the first shows which candidate each voted for in the Presidential elections and the second highlights the significant differences between Scottish, English and Welsh results in the Brexit referendum. It's hard to think of an alternative visualisation that makes more sense for this kind of data. --- ## Maps show where things happen <center>
</center> ??? Ultimately, maps show where things happen or have happened in the past. If the relative location of data points matters then it is more than likely that creating a map will be worth all of the complexities I've highlighted. This map helps us visualise the clusters of Earthquakes scattered around Fiji and their relative strength. It's difficult to think of an alternative chart that allows us to do both things at once. --- ## R is a complete GIS system .pull-left[ Software designed to work with geospatial data is called GIS (Geospatial Information System) software. Most dedicated GIS tools are expensive and extremely technical to use. R is without doubt a complete and powerful GIS system. We can use it to map or manipulate any and all geospatial datasets. ] .pull-right[ <br> <br> <center> <img src='images/sf-logo.gif' width="40%"> <img src='images/mapview-logo.gif' width="40%"> </center> ] ??? Okay! So I've taken you through the complexities of making maps, and very quickly argued that making maps is often worth the trouble. I want to finish by talking about GIS software. GIS stands for Geospatial Information Systems and any software designed to process and/or visualise geospatial data is classified as GIS software. Most dedicated GIS tools are expensive and require technical expertise in GIS data manipulation. R without a doubt provides a complete and very powerful GIS system for manipulating and visualising any GIS dataset you could come across and we can use all of the flexibility and friendliness of the tidyverse to make sense of the data. I'll finish up by saying every single map in these slides was created using R codeand the "Learn more" section will provide more details.